package com.example.supplychainbackend.entity.sales;

import com.example.supplychainbackend.tool.DateTimeUtils;
import jakarta.annotation.Nonnull;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

//命名为order建表时会报错，与sql中order关键字冲突
@Entity
@Table(name = "orders")
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class OrderEntity {
    @Id
    String id;

    @Nonnull
    String contractNumber;

    String salesOrderName;
    int orderType;
    int referenceDocType;
    String salesOrderType;
    String customer;
    String customerName;
    String orderDate;
    String requiredDeliveryDate;
    String firstSendTime;
    String latestResendTime;
    String company;
    String salesOrganization;
    String salesChannel;
    String productGroup;
    int invoiceQuantity;
    int invoiceAmount;
    int receivableQuantity;
    int receivableAmount;
    int receivedAmount;
    String createDate;
    String updateDate;
    @OneToOne(cascade = CascadeType.ALL,orphanRemoval = true)
    @JoinColumn(name = "sales_info_id")
    SalesInfoEntity salesInfo;

    String	userName;
    String	enterpriseName;
    Boolean	onChain;
    String	transactionId;
    String	commitTime;
    String	userId;
    String	enterpriseId;

    @Transient
    private static final AtomicInteger counter = new AtomicInteger(1);

    @PrePersist
    public void generateId() {
        if (id == null || id.equals("")) {
            id = String.format("%s%s%04d",
                    "SO", DateTimeUtils.getCurrentDateForID(), counter.getAndIncrement());
        }
        createDate = DateTimeUtils.getCurrentDateTime();
        updateDate = DateTimeUtils.getCurrentDateTime();
    }
    @PreUpdate
    public void onUpdate(){
        updateDate = DateTimeUtils.getCurrentDateTime();
    }
}
